﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleRest.Serialization.Xsd
{
    public class Element
    {
        public Element(){
            this.DefaultXmlNamespace = "xs";
        }
        public string DefaultXmlNamespace{get; set;}
        public UInt32 MinimumOccurances { get; set; }
        public UInt32 MaximumOccurances { get; set; }
        public string Name { get; set; }
        string Type { get; set; }
        bool IsNullable { get; set; }

        public override string ToString()
        {
            if (string.IsNullOrEmpty(this.Name))
                throw new Exception("Name cannot be null.");
            if (string.IsNullOrEmpty(DefaultXmlNamespace))
                throw new Exception("Default XML namespace cannot be null.");
            if (string.IsNullOrEmpty(this.Type))
                throw new Exception("Type cannot be null.");
            if (this.MinimumOccurances > this.MaximumOccurances)
                throw new Exception("The minimum occurances must be less than the maximum occurances.");

            StringBuilder sb = new StringBuilder();
            sb.Append("<").Append(DefaultXmlNamespace).Append(@":element");
            sb.Append(@" minOccurs=""").Append(this.MinimumOccurances.ToString()).Append(@"""");
            sb.Append(@" maxOccurs=""").Append(this.MaximumOccurances.ToString()).Append(@"""");
            if (this.IsNullable)
                sb.Append(@" nillable=""").Append(this.IsNullable.ToString()).Append(@"""");
            sb.Append(@" name=""").Append(this.Name).Append(@"""\n");
            sb.Append("</").Append(this.DefaultXmlNamespace).Append(">");

            return sb.ToString();
        }
    }
}
